﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using ConfigGen.ConsoleApp.Core.CommandLineOptions;
using ConfigGen.Utilities.Extensions.System;
using NUnit.Framework;

namespace ConfigGen.ConsoleApp.Core.Tests.CommandLineOptions
{
    [TestFixture]
    public class GenerateSpecifiedOnlyTests
    {
        [Test]
        public void ShortName()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            Assert.AreEqual("g", itemUnderTest.ShortName);
        }

        [Test]
        public void LongName()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            Assert.AreEqual("generate-specified-only", itemUnderTest.LongName);
        }

        [Test]
        public void ArgsUsage()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            var argsUsage = itemUnderTest.ArgsUsage;
            Assert.IsTrue(argsUsage.Contains("list"));
        }

        [Test]
        public void HelpText()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            var argsUsage = itemUnderTest.HelpText;
            Assert.IsFalse(argsUsage.IsNullOrEmpty());
        }

        [Test]
        public void ConsumeArgumentsFromQueue()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            var queue = new Queue<string>();
            queue.Enqueue("arg1");
            queue.Enqueue("arg2");
            itemUnderTest.ConsumeArgumentsFromQueue(queue);
            Assert.AreEqual(1, queue.Count, "Single arg should have been consumed from queue");
        }

        [Test]
        public void ConsumeArgumentsFromQueue_ThrowsIfNoArgs()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            var queue = new Queue<string>();
            Assert.Throws<ArgumentException>(() => itemUnderTest.ConsumeArgumentsFromQueue(queue));
        }

        [Test]
        public void ApplyOptionToSettings()
        {
            var itemUnderTest = new GenerateSpecifiedOnly();
            var queue = new Queue<string>();
            queue.Enqueue("arg1.1,arg1.2,arg1.3");
            queue.Enqueue("arg2");
            itemUnderTest.ConsumeArgumentsFromQueue(queue);
            
            var settings = new ConsoleAppPreferences();
            itemUnderTest.ApplyOptionToSettings(settings);
            
            Assert.IsNotNull(settings.SpecifiedMachines, "SpecifiedMachines: should not be null");
            Assert.AreEqual(3, settings.SpecifiedMachines.Length, "SpecifiedMachines.Length: incorrect");
            Assert.IsNotNull(settings.SpecifiedMachines.FirstOrDefault(m => m == "arg1.1"), "arg1.1 not found");
            Assert.IsNotNull(settings.SpecifiedMachines.FirstOrDefault(m => m == "arg1.2"), "arg1.2 not found");
            Assert.IsNotNull(settings.SpecifiedMachines.FirstOrDefault(m => m == "arg1.3"), "arg1.3 not found");
        }
    }
}
